package cn.seaboot.admin.reverse.web;

import cn.seaboot.admin.mvc.Result;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.admin.reverse.manager.JavaCodeBuilder;
import cn.seaboot.admin.reverse.manager.MappingManager;
import cn.seaboot.commons.file.MediaType;
import cn.seaboot.commons.file.ResponseWriter;
import freemarker.template.TemplateException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 逆向工程配置
 *
 * @author postm
 */
@Controller
@Tag(name = "逆向工程配置")
@RequestMapping("db/reverse")
public class ReverseCtrl {

    @Resource
    private JavaCodeBuilder javaCodeBuilder;

    @Resource
    private MappingManager mappingManager;

    /**
     * 重新载入数据
     *
     * @param schemaName 数据库名称
     * @return 操作结果
     */
    @ResponseBody
    @Operation(description = "重置映射配置")
    @Parameters(@Parameter(name = "schemaName", description = "数据库名"))
    @RequestMapping(value = "reset", method = RequestMethod.POST)
    public Result reset(@Params String schemaName) {
        mappingManager.rebuildTable(schemaName);
        return Result.succeed();
    }

    /**
     * 重新载入单表的数据
     *
     * @param schemaName 数据库名称
     * @param tableName  表名
     * @return 操作结果
     */
    @ResponseBody
    @Operation(description = "重新载入配置")
    @Parameters({
            @Parameter(name = "schemaName", description = "数据库名"),
            @Parameter(name = "tableName", description = "表名")
    })
    @RequestMapping(value = "rebuild", method = RequestMethod.GET)
    public Result rebuild(@Params String schemaName,
                          @Params String tableName) {
        mappingManager.rebuildTable(schemaName, tableName);
        return Result.succeed();
    }


    /**
     * 获取渲染代码
     *
     * @param groupName  代码生成策略
     * @param id         模版ID
     * @param schemaName 数据库名称
     * @param tableName  表名
     * @param escape     是否转义
     * @return 操作结果
     * @throws IOException       -
     * @throws TemplateException -
     */
    @ResponseBody
    @Operation(description = "生成代码")
    @Parameters({
            @Parameter(name = "id", description = "模版ID"),
            @Parameter(name = "schemaName", description = "数据库名"),
            @Parameter(name = "tableName", description = "表名")
    })
    @RequestMapping(value = "code", method = RequestMethod.GET)
    public String showCode(
            HttpServletResponse response,
            @RequestParam String id,
            @RequestParam String groupName,
            @Params String schemaName,
            @Params String tableName,
            @RequestParam(defaultValue = "true") Boolean escape) throws IOException, TemplateException {
        response.setContentType(MediaType.TEXT_PLAIN_VALUE);
        return javaCodeBuilder.genCode(id, groupName, schemaName, tableName, escape);
    }


    /**
     * 获取渲染代码
     *
     * @param groupName  代码生成策略
     * @param type       模版类型
     * @param schemaName 数据库名称
     * @param tableName  表名
     * @param escape     是否转义
     * @return 操作结果
     * @throws IOException       -
     * @throws TemplateException -
     */
    @ResponseBody
    @Operation(description = "写入代码")
    @Parameters({
            @Parameter(name = "groupName", description = "模版组别"),
            @Parameter(name = "type", description = "模版类型"),
            @Parameter(name = "schemaName", description = "数据库名"),
            @Parameter(name = "tableName", description = "表名")
    })
    @RequestMapping(value = "write", method = RequestMethod.GET)
    public Result writeCode(
            @RequestParam String groupName,
            @RequestParam String type,
            @Params String schemaName,
            @Params String tableName,
            @RequestParam(defaultValue = "false") Boolean escape) throws IOException, TemplateException {
        javaCodeBuilder.writeCode(type, groupName, schemaName, tableName, escape);
        return Result.SUCCEED;
    }
}
